<?php
namespace lib;

class PayUtils
{

	/**
	 * 把在数组中的参数拼接成一个字符串
	 * @param $para 
	 * return string
	 */
	static public function createLinkstring($para) {
		$arg = "";
		foreach ($para as $key => $val) {
			$arg .= $key . "=" . $val . "&";
		}
		// 省略最后一个字符串
		$arg = substr($arg, 0, -1);		// 因为最后有一个 & symbols

		return $arg;
	}

	/**
	 * 多使用了一个 urlencode 方法, 支持中文
	 */
	static public function createLinkstringUrlencode($para) {
		$arg = "";
		foreach ($para as $key => $val) {
			$arg .= $key . "=" . urlencode($val) . "&";
		}
		// 去掉最后一个 & sign.
		$arg = substr($arg, 0, -1);

		return $arg;
	}

	/**
	 * 过滤掉 sign, sign_type, 或者值(val)为空的 参数.
	 * @param $para
	 * return array
	 */
	static public function paraFilter($para) {
		$para_filter = array();
		foreach ($para as $key => $val) {
			if ($key == "sign" || $key == "sign_type" || $val == "") continue;
			else $para_filter[$key] = $para[$key];
		}
		return $para_filter;
	}

	/**
	 * @param $para
	 */
	static public function argSort($para) {
		// Sorts array in place by keys in ascending order.
		ksort($para);
		// Set the internal pointer of an array to its first element
		reset($para);
		return $para;
	}

	/**
	 * @param $prestr
	 * @param $key
	 */
	static public function md5Sign($prestr, $key) {
		$prestr = $prestr . $key;
		return md5($prestr);
	}

	/**
	 * @param $prestr
	 * @param $sign
	 * @param $key
	 */
	static public function md5Verify($prestr, $sign, $key) {
		$prestr = $prestr . $key;
		$mysgin = md5($prestr);

		if ($mysgin == $sign) {
			return true;
		} else {
			return false;
		}
	}
}
